package demo;

public class MySingleList implements IList {

    public static class ListNode {//外面是链表,里面要求是整体,即节点,则使用内部类
        public int val;
        public ListNode next;
        public ListNode(int val) {
            this.val = val;
        }
    }
    //链表的属性,头节点
    public ListNode head;
    public void createNode() {
        ListNode node1 = new ListNode(12);
        ListNode node2 = new ListNode(23);
        ListNode node3 = new ListNode(34);
        ListNode node4 = new ListNode(45);

        node1.next = node2;
        node2.next = node3;
        node3.next = node4;
        node4.next = null;

        this.head = node1;
    }

    /**
     * 头插
     * @param data
     */
    @Override
    public void addFirst(int data) {
        ListNode node = new ListNode(data);

        node.next = this.head;
        this.head = node;
    }

    /**
     * 尾插
     * @param data
     */
    @Override
    public void addLast(int data) {
        ListNode node = new ListNode(data);

        if(this.head == null) {
            this.head = node;
        }else {
            ListNode cur = this.head;
            while(cur.next != null) {
                cur = cur.next;
            }
            cur.next = node;
        }
    }

    /**
     * 在index下标位置插入节点(第一个节点为0)
     * @param index
     * @param data
     */
    @Override
    public void addIndex(int index, int data) {
        if(index < 0 || index > size()) {
            throw new IndexException("下标不合法!");
        }

        if(index == 0) {
            addFirst(data);
            return;
        }

        if(index == size()) {
            addLast(data);
            return;
        }
        //中间位置插入
        ListNode cur = searchPrevNode(index);
        ListNode node = new ListNode(data);

        node.next = cur.next;
        cur.next = node;
    }
    private ListNode searchPrevNode(int index) {
        ListNode cur = head;
        int count = 0;
        //找到下标前一个节点
        while(count != index) {
            cur = cur.next;
            count++;
        }

        return cur;
    }

    /**
     * 判断链表中是否存在val = key的节点
     * @param key
     * @return
     */
    @Override
    public boolean contains(int key) {
        if(head == null) {
            return false;
        }

        ListNode cur = head;

        while(cur != null) {
            if(cur.val == key) {
                return true;
            }
            cur = cur.next;
        }

        return false;
    }

    /**
     * 删除节点key
     * @param key
     */
    @Override
    public void remove(int key) {
        if(head == null) {
            return;
        }

        if(head.val == key) {
            head = head.next;
            return;
        }

        ListNode cur = findPrevKey(key);
        if(cur == null) {
            return;
        }
        cur.next = cur.next.next;
    }
    private ListNode findPrevKey(int key) {
        ListNode cur = head;
        while(cur.next != null) {
            if(cur.next.val == key) {
                return cur;
            }else {
                cur = cur.next;
            }
        }
        return null;
    }

    /**
     * 删除所有key节点
     *      1.删除除头节点以外节点
     *              使用两个节点进行处理(前驱 待删)
     *      2.处理头节点
     * @param key
     */
    @Override
    public void removeAllKey(int key) {
        if(head == null) {
            return;
        }
        //如果先处理head需要使用循环,因为可能头后面还是需要删的节点
//        while(head.val == key) {
//            head = head.next;
//        }

        ListNode cur = head;
        ListNode prev  = head;
        //处理后面节点
        while(cur != null) {
            if(cur.val == key) {
                prev.next = cur.next;
                cur = cur.next;
            }else {//cur.val != key
                prev = cur;
                cur = cur.next;
            }
        }
        //后面处理,使用if
        if(head.val == key) {
            head = head.next;
        }
    }

    /**
     * 求链表节点个数
     * @return
     */
    @Override
    public int size() {
        if(head == null) {
            return 0;
        }

        int count = 0;
        ListNode cur = head;

        while(cur != null) {
            count++;
            cur = cur.next;
        }

        return count;
    }

    @Override
    public void clear() {
        this.head = null;
    }

    /**
     * 打印节点
     */
    @Override
    public void display() {
        ListNode cur = head;
        while(cur != null) {
            System.out.print(cur.val + " ");
            cur = cur.next;
        }
        System.out.println();
    }
    public void display(ListNode newHead) {
        ListNode cur = newHead;
        while(cur != null) {
            System.out.print(cur.val + " ");
            cur = cur.next;
        }
        System.out.println();
    }
    /**
     * 反转链表
     */
    public ListNode reverseList() {
        /**
         * 1.头节点为空
         * 2.进行头插
         */
        //为空
        if(head == null) {
            return null;
        }
        //只有一个节点
        if(head.next == null) {
            return head;
        }
        //多个节点反转
        ListNode cur = head.next;
        head.next = null;

        while(cur != null) {
            ListNode curNext = cur.next;
            cur.next = head;
            head = cur;
            cur = curNext;
        }

        return head;
    }
    /**
     * 力扣->876.链表的中间节点
     *      快慢指针->原理:长度N,A以B的两倍速度前进,当走完全程时,B会在N/2位置
     *      1.定义快慢指针
     *      2.区分奇偶情况
     *      3.返回slow节点
     */
    public ListNode middleNode() {
        /**
         *快慢指针:相同一段距离N,当A以B2倍速度进行移动时,当A走完时,B必然在N的中间位置
         *       1.定义快慢指针
         *       2.区分奇偶节点问题
         */
        if(head == null) {
            return null;
        }
        ListNode slow = head;//慢指针
        ListNode fast = head;//快指针

        while(fast != null && fast.next  != null) {//不能反,可能导致null异常
            fast = fast.next.next;
            slow = slow.next;
        }

        return slow;
    }
    /**
     * 链表中倒数第k个节点->原理:长度为N,保持A(A先走k - 1步)B永远距离一致,当A走完N时,那么B位置处就是倒数第K个位置
     *      1.A先走k-1步
     *      2.保持距离,同时移动,
     *      3.当A走完时,B位置就是倒数第K个位置
     */
    public ListNode findKthToTail(int index) {
        if(head == null) {
            return null;
        }

        if(index <= 0) {
            return null;
        }
        //fast先移动k-1步
        ListNode fast = head;
        ListNode slow = head;
        int count = 0;
        while(count != index) {
            if(fast.next == null) {//证明K过大
                return null;
            }else {
                count++;
                fast = fast.next;
            }
        }
        //同时移动
        while(fast.next != null) {
            fast = fast.next;
            slow = slow.next;
        }
        return slow;
    }
}
